package com.lq.service;

import com.lq.common.SqlConstant;
import com.lq.utils.jdbc.Jdbc;
import com.lq.utils.jdbc.LqJdbcFactory;
import com.lq.utils.jdbc.LqPiLiang;
import org.apache.commons.collections.map.ListOrderedMap;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

public class InitService {


    /**
     * 创建数据库表
     */
    public static void createTable() {
        try {
            Jdbc.execute(SqlConstant.SQL_CREATE_RECORD_MQTT);
            Jdbc.execute(SqlConstant.SQL_CREATE_RECORD_ALARMS_MQTT);
            Jdbc.execute(SqlConstant.SQL_CREATE_GPS_MQTT);

            String sql = "insert ignore into ypsyscfg (ypsection, ypitemname, ypitemvalue ) values (?, ?, ?) ";
            List<Object[]> listObjects = new ArrayList();
            Object[] obj1 = new Object[]{"recordAlarms", "mqtt", 0};
            Object[] obj2 = new Object[]{"record", "mqtt", 0};
            Object[] obj3 = new Object[]{"gps", "mqtt", 0};
            listObjects.add(obj1);
            listObjects.add(obj2);
            listObjects.add(obj3);
            Jdbc.piliang(sql, new LqPiLiang() {
                @Override
                public void piliang(LqJdbcFactory jdbc) {
                    listObjects.forEach(l -> {
                        jdbc.executeS(l);
                    });
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化平台数据记录追踪表
     */
    public static void initTrackPingTai() {
        try {
            List<ListOrderedMap> list = Jdbc.find("select * from company_mark where state = ?", new Object[]{"1"});
            String inserSql = "insert ignore into track_pingtai (company_area_code, type, record_id ) values (?, ?, ?) ";
            if (list != null && list.size() > 0) {
                StringJoiner joiner = new StringJoiner(",");
                List<Object[]> listObjects = new ArrayList();
                for (int i = 0; i < list.size(); i++) {
                    String companyKey = list.get(i).get("company_key").toString();
                    joiner.add("\'" + companyKey + "\'");
                    Object[] obj1 = new Object[]{companyKey, "record", 0};
                    Object[] obj2 = new Object[]{companyKey, "gps", 0};
                    Object[] obj3 = new Object[]{companyKey, "recordAlarms", 0};
                    Object[] obj4 = new Object[]{companyKey, "events", 0};
                    Object[] obj5 = new Object[]{companyKey, "task", 0};
                    listObjects.add(obj1);
                    listObjects.add(obj2);
                    listObjects.add(obj3);
                    listObjects.add(obj4);
                    listObjects.add(obj5);
                }
                List checkList = Jdbc.find("select * from track_pingtai where company_area_code  in (" + joiner.toString() + ")");
                if (checkList.isEmpty()) {
                    Jdbc.piliang(inserSql, new LqPiLiang() {
                        @Override
                        public void piliang(LqJdbcFactory jdbc) {
                            listObjects.forEach(l -> {
                                jdbc.executeS(l);
                            });
                        }
                    });
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
